import axios, { AxiosError, type AxiosRequestHeaders, type AxiosResponse } from 'axios';

/* 定义response对象的data接口 */
interface ResponseData<T> {
    code: number;
    data: T;
    message: string;
}

// 配置新建一个 axios 实例
const formDataReq = axios.create({
    baseURL: '/kl',
    timeout: 50000,
});

const token = sessionStorage.getItem('Authorization')

// 添加请求拦截器
formDataReq.interceptors.request.use(
    (config) => {
        const token = sessionStorage.getItem('Authorization')
        if (token) {
            (config.headers as AxiosRequestHeaders).Authorization = token
        } else {
            (config.headers as AxiosRequestHeaders).Authorization = `309_token`
        }
        config.headers['Content-Type'] = 'multipart/form-data'
        return config;
    }
);

// 添加响应拦截器
formDataReq.interceptors.response.use(
    /* 约束一下response */
    (response: any) => {
        return response.data
        // let res = response.data;
        // if (res.code === 200) {
        // 	return res.data
        // } else {
        // 	return Promise.reject(response.data.message)
        // }
    }
);

export default formDataReq;
